package org.vectrics.user.client;


import org.vectrics.VectricsException;
import org.vectrics.common.dom.MarshalUtils;
import org.vectrics.client.webclient.*;
import org.vectrics.user.Person;
import org.vectrics.user.Person;
import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class FindUserDetailsCommand extends ServerCommandImpl {
	private transient static Logger log = Logger.getLogger(FindUserDetailsCommand.class);
	//private String userId;
	//private Properties userProperties = null;
	//private Properties contactInfoProperties = null;
	private Long personId = null;
	private Person person = null;

	public FindUserDetailsCommand(Long personId) {
		super();
		this.personId = personId;
	}

	public String getCommandCode() {
		return("findUserDetails");
	}
	
	
	protected void prepareRequestData(Element requestElement) {
		log.debug("getParameters() - person id = " + personId);
		Element userElement = requestElement.getOwnerDocument().createElement("person");
		requestElement.appendChild(userElement);
		userElement.setAttribute("id", personId.toString());
	}


	protected void processResponse(Element dataElement) {
		log.debug("processResponse() - translating XML document into user list");
		NodeList userElements = dataElement.getElementsByTagName("user"); 
		for (int i = 0; i < userElements.getLength(); i++) {
			Element userElement = (Element)userElements.item(i);
			
			try {
				MarshalUtils marshaller = MarshalUtils.getInstance();
				person = (Person)marshaller.elementToObject(userElement, Person.class);//UserMarshalling.elementToUser(userElement);
				log.debug("processResponse - user.middleName = " + person.getMiddleName());
			}
			catch (Exception de) {
				log.error("DataException - while translating DOM element to user object", de);
			}
		}
	}


	/**
	 * @return Returns the user.
	 */
	public Person getPerson() {
		return person;
	}

	/**
	 * @param user The user to set.
	 */
	public void setPerson(Person person) {
		this.person = person;
	}

}
